/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.jims.master.modules.doctor.netHosptial.service;

import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.RestUtils;
import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.doctor.netHosptial.dao.NetTestItemsDao;
import com.jims.master.modules.doctor.netHosptial.dao.NetTestMasterDao;
import com.jims.master.modules.doctor.netHosptial.entity.NetClinicMaster;
import com.jims.master.modules.doctor.netHosptial.entity.NetPatMaster;
import com.jims.master.modules.doctor.netHosptial.entity.NetTestItems;
import com.jims.master.modules.doctor.netHosptial.entity.NetTestMaster;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 检验主记录Service
 * @author qlx
 * @version 2016-09-10
 */
@Service
@Transactional(readOnly = true)
public class NetTestMasterService extends CrudService<NetTestMasterDao, NetTestMaster> {

    @Autowired
    private NetPatMasterService netPatMasterService;

    @Autowired
    private NetClinicMasterService netClinicMasterService;

    @Autowired
    private NetOrdersService hisOutpOrdersService;

    @Autowired
    private NetTestItemsDao hisTestItemsDao;

    /**
     * 获得患者未传送的信息
     * @param-patientId
     * @return
     */
    public List<NetTestMaster> getInfoToSend(NetTestMaster netTestMaster){
        return dao.getInfoToSend(netTestMaster);
    };

    //查询最大testNo
    public String findMaxNetTestMaster() {
        return dao.findMaxNetTestMaster();
    }

    //调用接口方法（itemcode表示map的value值，keyName表示map中key的名称）
    public List<BaseDto> getExamList(String url, String itemcode, String keyName) {
        List<BaseDto> patientList = new ArrayList<BaseDto>();
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put(keyName, itemcode);
        params.put("itemClass", "C");
        RestUtils<BaseDto, HashMap> restUtils = new RestUtils<BaseDto, HashMap>();
        try {
            Map<String, Object> map = restUtils.findByList(url, BaseDto.class, params, params);
            patientList = (List<BaseDto>) map.get("body");
        } catch (Exception e) {
            e.getMessage();
        }
        return patientList;
    }

    //改变发送标志
    @Transactional(readOnly = false)
    public Integer send(NetTestMaster netTestMaster){
        return dao.send(netTestMaster);
    }

    /***互联网医院NetTestMaster保存
     * patientId、clinicId都已经生成了     *
     * @param labTestMaster
     */
    @Transactional(readOnly = false)
    public void saveVillageAll(NetTestMaster labTestMaster){
        NetPatMaster netPatMaster = netPatMasterService.findIndexInfoByPatientId(labTestMaster.getPatientId());
        NetClinicMaster netClinicMaster = netClinicMasterService.get(labTestMaster.getClinicId());
        //labTestMaster
        labTestMaster.preInsert();
        //TEST_NO at+num a:医院序号、t申请号字段首字母
        String testNo = findMaxNetTestMaster();
        if (StringUtils.isEmpty(testNo)) {
            testNo = "AT1";
        } else {
            testNo = "AT" + testNo;
        }
        labTestMaster.setTestNo(testNo);
        //VISIT_ID 门诊
        String visitId = "0";
        labTestMaster.setVisitId(visitId);
        //labTestMaster.setOrgId("");
        //NAME, NAME_PHONETIC, SEX, age,
        labTestMaster.setName(netPatMaster.getName());
        labTestMaster.setNamePhonetic("");
        labTestMaster.setSex(netPatMaster.getSex());
        if(StringUtils.isNotEmpty(netPatMaster.getAge()))
        labTestMaster.setAge(netPatMaster.getAge());
        //charge_type
        labTestMaster.setChargeType(netPatMaster.getChargeType());
        //UserUtils.getUser().getOfficeId()
        labTestMaster.setOrderingDept(netClinicMaster.getVisitDept());
        labTestMaster.setOrderingDeptName(netClinicMaster.getVisitDeptName());
        //todo
        //labTestMaster.setOrderingProvider(UserUtils.getBaseInfo().getHisloginname());
        //labTestMaster.setOrderingProviderName(UserUtils.getBaseInfo().getName());
        labTestMaster.setRequestedDateTime(netClinicMaster.getVisitDate());
        //performedBy
        //result_status ward_code
        labTestMaster.setResultStatus("1");
        labTestMaster.setRemarks("0");
        List<NetTestItems> labTestItemsList = labTestMaster.getItemList();
        //costs charges billingIndicator
        Float cost = 0f;
        for(NetTestItems entity_cost:labTestItemsList){
            cost=cost+Float.valueOf(entity_cost.getPrice());
        }
        cost = (float)(Math.round(cost*100))/100;
        labTestMaster.setCosts(cost.toString());
        labTestMaster.setCharges(cost.toString());
        labTestMaster.setBillingIndicator("0");
        //serial_no a+num a:医院序号
        String serialNo = "";
        serialNo = hisOutpOrdersService.findMaxOutpOrders();
        if (StringUtils.isEmpty(serialNo)) {
            serialNo = "A1";
        } else {
            serialNo = "A" + serialNo;
        }
        dao.insert(labTestMaster);
        for (int i = 0; i < labTestItemsList.size(); i++) {
            NetTestItems labTestItems = labTestItemsList.get(i);
            //hisExamItems.setAppointsId(hisExamAppoints.getId());
            labTestItems.setTestNo(testNo);
            labTestItems.setItemNo(String.valueOf(i + 1));
            labTestItems.preInsert();
            hisTestItemsDao.insert(labTestItems);
        }
    }

    /**
     * 由挂号表关联检验
     * @param-clinicId
     * @return
     */
    public List<NetTestMaster> getByClinicId(NetTestMaster netTestMaster){
        return dao.getByClinicId(netTestMaster);
    }

    /***
     * 删除
     * 新版本不涉及自动生成的业务表
     * @param netTestMaster
     */
    @Transactional(readOnly = false)
    public void deleteAll(NetTestMaster netTestMaster){
        dao.delete(netTestMaster);
    }

    public NetTestMaster getInfoByTestNo(NetTestMaster netTestMaster){
        return dao.getInfoByTestNo(netTestMaster);
    }

    /**
     * 查询患者所有的检验记录
     * @param-patientId
     * @param-visitId
     * @return
     */
    public List<NetTestMaster> findAllTest(NetTestMaster netTestMaster){
        return dao.findAllTest(netTestMaster);
    }

    /**
     * 得到病人的所有检查
     * @param-patientId
     * @param-visitId
     * @return
     */
    public List<BaseDto> getMaster(NetTestMaster netTestMaster){
        return dao.getMaster(netTestMaster);
    }
}